/*
 * Created on 25.09.2006 by leck
 *
 */
package de.devisnik.eidle.flashlight;

import org.eclipse.core.runtime.Assert;

public class Star extends AbstractShape {

    private final int[] itsPoints;

    public Star(int corners, int radius) {
        Assert.isTrue(corners >= 5);
        Assert.isTrue(corners%2 == 1);
        itsPoints = createPolygon(corners, radius);
    }

    public int[] getPointArray(int centerX, int centerY) {
        int[] pointsCopy = clonePoints(itsPoints);
        translate(pointsCopy, centerX, centerY);
        return pointsCopy;
    }

    private int[] createPolygon(int corners, int radius) {
        double angle = 2 * Math.PI / corners;
        int[] polygon = new int[2 * corners];
        if (corners % 2 == 0) {
            for (int i = 0; i < polygon.length; i += 2) {
                int index = (2*i) % polygon.length;
                polygon[i] = (int) (Math.cos(index / 2 * angle) * radius);
                polygon[i + 1] = (int) (Math.sin(index / 2 * angle) * radius);
            }
        }
        else {            
            for (int i = 0; i < polygon.length; i += 2) {
                int index = (2*i) % polygon.length;
                polygon[i] = (int) (Math.sin(index / 2 * angle) * radius);
                polygon[i + 1] = (int) (Math.cos(index / 2 * angle) * radius * (-1));
            }
        }
        return polygon;
    }
}
